<?php


namespace app\bonus\controller;



use app\bonus\logic\CouponLogic;
use app\bonus\logic\WapshopLogic;
use app\bonus\model\WapCoupon;
use app\store\controller\Controller;

class Wapshop extends Controller
{

    /**
     * 获取列表
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function lists(){

        $wdata = [];
        $list = \app\bonus\model\WapShop::getListData($wdata);
        $this->assign('list',$list);
        return $this->fetch();
    }

    /**
     * 添加商铺
     * @return mixed
     */
    public function add(){
        $param = $this->postData("data");
        $logic = new WapshopLogic();

        //验证数据
        if($this->request->isAjax()){
            //验证数据
            $yz = $logic->verifyFun($param);
            if($yz['code'] > 0){
                return $this->renderError( $yz['meg']);
            }
            $rid = $logic->addData($param);
            if($rid){
                return $this->renderSuccess('添加成功', url("lists"));
            }else{
                return $this->renderError( '添加失败');
            }
        }else{
            $region = $logic->getRegion(0);
            $this->assign('region',$region);
            return $this->fetch();
        }

    }

    public function edit($id){
        //$id = $this->getData("id");
        $logic = new WapshopLogic();
        if($this->request->isAjax()){
            $param = $this->postData("data");
            //验证数据
            $yz = $logic->verifyFun($param,'edit');
            if($yz['code'] > 0){
                return $this->renderError( $yz['meg']);
            }
            $rid = $logic->editData($param);
            if($rid){
                return $this->renderSuccess('编辑成功', url("lists"));
            }else{
                return $this->renderError( '编辑失败');
            }
        }
        //获取数据
        $list = $logic->getData($id);

        $region = $logic->getRegion(0);
        $cityRegion = $logic->getRegion($list['province']);
        $districtRegion = $logic->getRegion($list['city']);

        $this->assign('list',$list);
        $this->assign('region',$region);
        $this->assign('cityRegion',$cityRegion);
        $this->assign('districtRegion',$districtRegion);
        return $this->fetch();
    }

    public function editpwd($id){
        $logic = new WapshopLogic();
        if($this->request->isAjax()){
            $param = $this->postData("data");
            //验证数据
            $yz = $logic->verifyPwdFun($param,'edit');
            if($yz['code'] > 0){
                return $this->renderError( $yz['meg']);
            }
            $rid = $logic->editPwdData($param);
            if($rid){
                return $this->renderSuccess('编辑成功', url("lists"));
            }else{
                return $this->renderError( '编辑失败');
            }
        }
        $this->assign('id',$id);
        $list = $logic->getData($id);
        $this->assign('list',$list);
        return $this->fetch();
    }

    public function delete($id){
        $red = \app\bonus\model\WapShop::where(['id'=>$id])->delete();
        if ($red) {
            return $this->renderSuccess('删除成功');
        }else{
            return $this->renderError('删除失败');
        }
    }

    /**
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function ajaxRegion(){
        $logic = new WapshopLogic();
        $id = request()->param("id");
        $region = [];
        if($id){
            $region = $logic->getRegion($id);
        }
        return $this->renderJson(1,"","",$region);
    }

    /**
     * 列表也
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function couponlists(){
        $wdata = [];
        $logic = new CouponLogic();
        $list = $logic->getDataList($wdata);
        $this->assign('list',$list);
        return $this->fetch("coupon/lists");
    }

    public function couponadd(){
        $logic = new CouponLogic();
        if($this->request->isAjax()){
            $param = $this->postData("data");
            //验证数据
            $yz = $logic->verifyFun($param,'edit');
            if($yz['code'] > 0){
                return $this->renderError($yz['meg']);
            }
            $rid = $logic->addData($param);
            if($rid){
                return $this->renderSuccess('添加成功', url("couponlists"));
            }else{
                return $this->renderError( '添加失败');
            }
        }
        //查询商铺名称
        $shopData = (new WapshopLogic())->getListData();
        $this->assign('shopData',$shopData);
        $this->assign('coupon_color',$logic->coupon_color);
        $this->assign('lq_type',$logic->lq_type);
        $this->assign('coupon_type',$logic->coupon_type);
        $this->assign('expire_type',$logic->expire_type);
        $this->assign('apply_range',$logic->apply_range);
        $this->assign('bear_type',$logic->bear_type);
        return $this->fetch("coupon/add");
    }

    public function couponedit($id){
        $logic = new CouponLogic();
        if($this->request->isAjax()){
            $param = $this->postData("data");
            //验证数据
            $yz = $logic->verifyFun($param);
            if($yz['code'] > 0){
                return $this->renderError($yz['meg']);
            }
            $rid = $logic->editData($param);
            if($rid){
                return $this->renderSuccess('编辑成功', url("couponlists"));
            }else{
                return $this->renderError( '编辑失败');
            }
        }
        $list = WapCoupon::getDataOne(['coupon_id'=>$id]);
        //查询商铺名称
        $shopData = (new WapshopLogic())->getListData();
        $this->assign('shopData',$shopData);
        $this->assign('coupon_color',$logic->coupon_color);
        $this->assign('lq_type',$logic->lq_type);
        $this->assign('coupon_type',$logic->coupon_type);
        $this->assign('expire_type',$logic->expire_type);
        $this->assign('apply_range',$logic->apply_range);
        $this->assign('bear_type',$logic->bear_type);
        $this->assign('list',$list);
        return $this->fetch("coupon/edit");
    }

    public function coupondelete($id){
        $red = WapCoupon::where(['coupon_id'=>$id])->delete();
        if ($red) {
            return $this->renderSuccess('删除成功');
        }else{
            return $this->renderError('删除失败');
        }
    }
}